package lbj.cyw.dp;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/*
* 139. 单词拆分
* */
public class WordBreak139 {
    public boolean wordBreak(String s, List<String> wordDict) {
        boolean[] dp=new boolean[s.length()+1];
        // 使用set就不用循环判断子串跟wordDict里的每一个相等了
//        Set<String> set = new HashSet<>();
//        for (String s1 : wordDict) {
//            set.add(s1);
//        }
        dp[0]=true;
        for (int i = 1; i <= s.length(); i++) {
            for (int j = 0; j < wordDict.size();j++) {
                String sub=wordDict.get(j);
                if(i>=sub.length() && dp[i-sub.length()] && sub.equals(s.substring(i - sub.length(), i))){
                    dp[i]=true;
                    break;
                }
            }
        }
        return dp[s.length()];
    }
}
